package com.lansetech.framework.redis.peak.shaving;

import java.util.ArrayList;
import java.util.List;

public class ConsumerGroup {

    private final List<Thread> threads;

    private final PeakShavingRunnableContext context;

    public ConsumerGroup(PeakShavingRunnableContext context, List<Thread> threads) {
        this.threads = new ArrayList<Thread>(threads.size());
        this.threads.addAll(threads);
        this.context = context;
    }

    public void start() {
        if(threads.isEmpty()){
            throw new RuntimeException("因为没有可供执行的线程，因此无法启动消费者组");
        }
        this.context.setState(RunnableState.START);
        for (Thread thread : threads) {
            thread.start();
        }
    }

    public void shutdown() {
        context.setState(RunnableState.STOP);
    }
}
